package com.quo.controller;

import java.io.IOException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.quo.entity.Product;
import com.quo.entity.ProductExport;
import com.quo.entity.ProductSeries;
import com.quo.entity.ProductSeries2;
import com.quo.entity.ProductType;
import com.quo.entity.Productdel;
import com.quo.exceptions.CommonException;
import com.quo.service.ProductSeriesService;
import com.quo.service.ProductService;
import com.quo.utils.ExcelWriter;
import com.quo.utils.Result;
import com.quo.utils.ResultCode;

/**
 * 
 * @author 韩宛廷
 *
 */
@RestController("productSeriesController")
@RequestMapping(value = "/api")
public class ProductSeriesController {

	@Autowired
	public ProductSeriesService psService;

	@RequestMapping(value = "/product-series", method = RequestMethod.GET)
	public Result findAll() {
		List<ProductSeries> pslist = psService.getAll();
		Result result = new Result(ResultCode.SUCCESS);
		System.out.println(pslist);
		result.setData(pslist);
		return result;
	}

	@RequestMapping(value = "/product-series/{psid}", method = RequestMethod.GET)
	public Result findByPsid(@PathVariable(value = "psid") int psid) {

		ProductSeries2 productseries = psService.getByPsid(psid);
		Result result = new Result(ResultCode.SUCCESS);
		result.setData(productseries);
		return result;

	}

	@RequestMapping(value = "/product-series/{psid}", method = RequestMethod.PUT)
	public Result update(@PathVariable(value = "psid") int psid, @RequestBody ProductSeries2 productseries) {
		// 业务操作
		String notes = productseries.getNotes();
		int notelen = notes.length();
		if (notelen > 10) {
			return new Result(ResultCode.NOLENGTH);
		} else {
			productseries.setPsid(psid);
			psService.update(productseries);
			return new Result(ResultCode.SUCCESS);
		}

	}

	@RequestMapping(value = "/product-series/{psid}", method = RequestMethod.DELETE)
	public Result delete(@PathVariable(value = "psid") int psid) {
		psService.deleteById(psid);
		return new Result(ResultCode.SUCCESS);
	}

	@RequestMapping(value = "/product-series-del-enable", method = RequestMethod.GET)
	public Result seriesDelEnable() {
		List<Productdel> data = new ArrayList<>();
		List<ProductSeries> pslist = psService.getAll();
		for (ProductSeries pro : pslist) {
			Productdel prodel = new Productdel(pro.getPsid(), psService.getDelEnableByPsid(pro.getPsid()));
			data.add(prodel);
		}

		Result result = new Result(ResultCode.SUCCESS);
		result.setData(data);
		return result;
	}

	@RequestMapping(value = "/product-series-del", method = RequestMethod.POST)
	public Result seriesDelAll(@RequestBody int[] psids) throws CommonException {

		psService.seriesDelAll(psids);

		return new Result(ResultCode.SUCCESS);
	}

	@RequestMapping(value = "/product-series", method = RequestMethod.POST)
	public Result save(@RequestBody ProductSeries2 productseries) {
		/*
		 * ProductSeries2 ps2=new ProductSeries2(); ps2.setPsname(ps2.getPsname());
		 * ps2.setTid(ps2.getTid()); ps2.setNotes(ps2.getNotes());
		 */

		String notes = productseries.getNotes();
		int notelen = notes.length();
		if (notelen > 10) {
			return new Result(ResultCode.NOLENGTH);
		} else {
			psService.save(productseries);
			return new Result(ResultCode.SUCCESS);
		}

	}

	/**
	 * author:韩宛廷 产品系列导出
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "/productseries-export", method = RequestMethod.POST)
	public void ProductSeriesExport(@RequestBody int[] psids, HttpServletRequest request,
			HttpServletResponse response) {
		String[] arrays = { "シリーズ番号", "シリーズ名", "タイプ", "付属" };

		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-disposition",
				"attachment;filename=Product-" + System.currentTimeMillis() + ".xlsx");
		List<ProductSeries> psList = psService.getByPsids(psids);
		System.out.println(psList);
		ExcelWriter<ProductSeries> ew = new ExcelWriter<>();
		XSSFWorkbook workbook = ew.getWorkbook(psList, "产品信息", ProductSeries.class, arrays);
		ServletOutputStream outputStream = null;
		try {
			outputStream = response.getOutputStream();
			workbook.write(outputStream);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				workbook.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				outputStream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
